/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package	dao;

import Hibernate.HibernateUtil;
import pojo.Author;
import pojo.Publisher;
import pojo.Translator;


import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class PublisherDAO extends AbstractDAO {

	@Override
	protected Class getPOJOClass() {
		// TODO Auto-generated method stub
		return Publisher.class;
	}
	
    public Publisher getPublisherByID(int id){
        Publisher publisher = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            publisher = (Publisher)session.get(Publisher.class, id);
        }
        catch(HibernateException ex){
            
        }
        finally{
            session.close();
        }
        return publisher;
    }
    public int insert(String publisherName){
    	Publisher publisher = new Publisher();
    	publisher.setName(publisherName);
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(publisher);
			transaction.commit();
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
		}
		finally {
			session.close();
			return publisher.getPublisherId();
		}
    }
    public static synchronized Publisher checkAndInsert(String publisherName){
    	//boolean isFind = false;
    	//int publisherID = -1;
    	PublisherDAO publisherdao = new PublisherDAO();
    	List<Publisher> publishers = publisherdao.getList();
    	if(publishers.isEmpty()){
    		Publisher publisher = new Publisher();
    		publisher.setName(publisherName);
    		Session session = HibernateUtil.getSessionFactory().openSession();
    		Transaction transaction = null;
    		try {
    			transaction = session.beginTransaction();
    			session.save(publisher);
    			transaction.commit();
    		} catch (HibernateException ex) {
    			// Log the exception
    			transaction.rollback();
    		}
    		finally {
    			session.close();
    			//publisherID = publisher.getPublisherId();
    			return publisher;
    		}
    	}
    	else{
    		int size = publishers.size();
    		for(int i = 0; i < size; i++){
    			if(java.text.Normalizer.normalize(publisherName, java.text.Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+","").compareTo(java.text.Normalizer.normalize(publishers.get(i).getName(), java.text.Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+","")) == 0){
    				//publisherID = publishers.get(i).getPublisherId();
    				//isFind = true;
    				//break;
    				return publishers.get(i);
    			}
    		}
    		//if(!isFind){
    		Publisher publisher = new Publisher();
       		publisher.setName(publisherName);
       		Session session = HibernateUtil.getSessionFactory().openSession();
       		Transaction transaction = null;
       		try {
       			transaction = session.beginTransaction();
       			session.save(publisher);
       			transaction.commit();
       		} catch (HibernateException ex) {
       			// Log the exception
       			transaction.rollback();
        	}
        	finally {
        		session.close();
       			//publisherID = publisher.getPublisherId();
        		return publisher;
       		}
    		//}
    	}
    	//return publisherID;
    }
    
    
    
    public List<Author> getPublisher(int n)
    {
    	List<Author> ds= null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		String hql = "select s from Publisher s join s.books b group by s.publisherId order by count(b) desc " ;
		Query query = session.createQuery(hql);
		
		query.setMaxResults(n);
		ds =  query.list();
		session.close();
    	return ds;
    }
}
